package com.api.gbhg.huangwm.cron;

import com.customization.commons.LogTool;
import com.weaver.procedure.hrmrolemembers.Hrmrolemembers_insert;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author huangwm
 * @Date 2022-04-19
 * @Version 1.0
 * @Description TODO 获取分部矩阵中的会计人员列添加到会计角色中
 */
public class SetRoleMenbersFormMatrix extends BaseCronJob {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/cron/SetRoleMenbersFormMatrix",false);

    public String roleid_kj = "";
    public String getRoleid_kj(){
        return roleid_kj;
    }
    public void setRoleid_kj(String roleid_kj){
        this.roleid_kj = roleid_kj;
    }
    public String roleid_cn = "";
    public String getRoleid_cn(){
        return roleid_cn;
    }
    public void setRoleid_cn(String roleid_cn){
        this.roleid_cn = roleid_cn;
    }
    @Override
    public void execute() {
        setKJ();;
        setCN();
    }

    public void setKJ() {
        logTool.writeLog("roleid_kj:"+this.roleid_kj);
        if(!"".equals(this.roleid_kj)) {
            RecordSet rs = new RecordSet();
            rs.execute("select dwyscwry from Matrixtable_1");
            StringBuffer sb = new StringBuffer();
            while (rs.next()){
                if(!"".equals(rs.getString("dwyscwry"))){
                    sb.append(rs.getString("dwyscwry")+",");
                }
            }
            String[] arr = sb.toString().split(",");
            List<String> members = new ArrayList<>();
            for (String s : arr){
                System.err.println(s);
                if(!s.isEmpty() && !members.contains(s)){
                    members.add(s);
                }
            }
            logTool.writeLog("矩阵人员："+members.size());
            RecordSet rs2 = new RecordSet();
            List<List> lists = new ArrayList<>();
            for (String s : members){
                List<String> params = new ArrayList<>();
                rs2.execute("select * from HrmRoleMembers where roleid="+this.roleid_kj+" and resourceid="+s);
                if(rs2.next()){
                    continue;
                }
                params.add(this.roleid_kj);
                params.add(s);
                params.add("2");
                params.add("1");
                params.add(getUUID());
                lists.add(params);
            }
            if(lists.size()>0){
                logTool.writeLog("同步数量："+lists.size());
                try{
                    boolean result = rs.executeBatchSql("insert into HrmRoleMembers(roleid,resourceid,rolelevel,resourcetype,uuid)values(?,?,?,?,?)",lists);
                    logTool.writeLog("同步结果："+result);
                }catch (Exception e){
                    e.printStackTrace();
                    logTool.writeLog(e.getMessage());
                }

            }else{
                logTool.writeLog("本次同步未有差异数据");
            }

        }
    }

    public void setCN() {
        logTool.writeLog("roleid_cn:"+this.roleid_cn);
        if(!"".equals(this.roleid_cn)) {
            RecordSet rs = new RecordSet();
            rs.execute("select shcwcn from Matrixtable_1");
            StringBuffer sb = new StringBuffer();
            while (rs.next()){
                if(!"".equals(rs.getString("shcwcn"))){
                    sb.append(rs.getString("shcwcn")+",");
                }
            }
            String[] arr = sb.toString().split(",");
            List<String> members = new ArrayList<>();
            for (String s : arr){
                System.err.println(s);
                if(!s.isEmpty() && !members.contains(s)){
                    members.add(s);
                }
            }
            logTool.writeLog("矩阵人员："+members.size());
            RecordSet rs2 = new RecordSet();
            List<List> lists = new ArrayList<>();
            for (String s : members){
                List<String> params = new ArrayList<>();
                rs2.execute("select * from HrmRoleMembers where roleid="+this.roleid_cn+" and resourceid="+s);
                if(rs2.next()){
                    continue;
                }
                params.add(this.roleid_cn);
                params.add(s);
                params.add("2");
                params.add("1");
                params.add(getUUID());
                lists.add(params);
            }
            if(lists.size()>0){
                logTool.writeLog("同步数量："+lists.size());
                try{
                    boolean result = rs.executeBatchSql("insert into HrmRoleMembers(roleid,resourceid,rolelevel,resourcetype,uuid)values(?,?,?,?,?)",lists);
                    logTool.writeLog("同步结果："+result);
                }catch (Exception e){
                    e.printStackTrace();
                    logTool.writeLog(e.getMessage());
                }

            }else{
                logTool.writeLog("本次同步未有差异数据");
            }

        }
    }

    //生成UUID
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return uuid;
    }
}
